package com.qwer.controller.accesscontrol;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qwer.controller.comm.BaseController;
import com.qwer.model.accesscontrol.AccFileInfo;
import com.qwer.service.accesscontrol.AccFileService;

@Controller
@RequestMapping("/accFile")
public class AccFileController extends BaseController{
    
    @Autowired
    private AccFileService AccFileService;
    
    
    @RequestMapping(value = { "/main"})
    public String index(Model model,HttpServletRequest req) {
        model.addAttribute("user", getLoginUser(req));
        return "page/accesscontrol/accFile/main";
    }
    
    @RequestMapping(value = { "/test"})
    public String test(Model model,HttpServletRequest req) {
        model.addAttribute("user", getLoginUser(req));
        return "page/accesscontrol/accFile/testmain";
    }
    
    
    
    @RequestMapping(value = { "/edit"})
    public String zpIndex(Integer id,Model model) {
        AccFileInfo data = new AccFileInfo();
        if(id!=null){
            data = AccFileService.findById(id);
        }
        model.addAttribute("model", data);
        return "page/accesscontrol/accFile/edit";
    }
    
    
    @RequestMapping("/delete")
    @ResponseBody
    public int delete(HttpServletRequest request,Integer id){
        try {
            AccFileService.deleteById(id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }
    
    @RequestMapping("/save")
    @ResponseBody
    public int save(HttpServletRequest request,AccFileInfo model){
        try {
            model.setUserId(getLoginUser(request).getId());
            AccFileService.save(model);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }
    
    
    @RequestMapping(value = "/getPageDatas")
    @ResponseBody
    public String getPageDatas(@RequestParam(value = "page", defaultValue = "1") Integer page,
        @RequestParam(value = "size", defaultValue = "10") Integer size,AccFileInfo data,HttpServletRequest req,String type){
        Sort sort = new Sort(Direction.DESC, "id");
        if(!"getAll".equals(type)){
            data.setUserId(getLoginUser(req).getId());
        }
        PageRequest request = new PageRequest(page - 1, size,sort);
        Page<AccFileInfo> zpModels = AccFileService.findList(request,data);
       
        
        List<AccFileInfo> list = zpModels.getContent();
        
        //将数据准备好layUI的格式
        JSONObject jobj = new JSONObject();
        jobj.put("code",0);
        jobj.put("msg","");
        jobj.put("count",zpModels.getTotalElements());
        jobj.put("data",JSON.parseArray(JSON.toJSONString(list)));
        System.out.println(jobj.toString());
        return jobj.toString();
    }
}
